package review.system.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import review.dao.CompanyDAO;
import review.entity.Company;
import review.entity.Department;

import java.util.List;

@Service
@Transactional
public class CompanyServiceImpl implements CompanyService {

  @Autowired
  private CompanyDAO companyDAO;
  @Autowired
  private DepartmentService departmentService;

  // 添加单位
  public void addCompany(Company co) {
    if (!StringUtils.hasText(co.getName())) {
      throw new RuntimeException("单位名称不能为空");
    }
    if (existsByName(co.getName(), co.getParent())) {
      throw new RuntimeException("单位名称不能重复");
    }
    companyDAO.save(co);
    co.setOrderindex(co.getId());
  }

  public boolean existsByName(String name, Integer pid) {
    boolean result = false;
    Company coExample = new Company();
    coExample.setName(name);
    coExample.setParent(pid);
    Example<Company> example = Example.of(coExample);
    result = companyDAO.exists(example);
    return result;
  }

  // 修改单位
  public void updateCompany(Company co) {
    if (!StringUtils.hasText(co.getName())) {
      throw new RuntimeException("单位名称不能为空");
    }
    if (co.getId() == null) {
      throw new RuntimeException("修改的单位ID为空");
    }
    Company co1Example = new Company();
    co1Example.setName(co.getName());
    co1Example.setParent(co.getParent());
    Example<Company> example = Example.of(co1Example);
    List<Company> list = companyDAO.findAll(example);
    for (Company company : list) {
      if (company.getId().intValue() != co.getId()) {
        throw new RuntimeException("单位名称重复");
      }
    }
    companyDAO.saveAndFlush(co);
  }

  // 删除单位
  public void delectCompany(Company co) {
    if (co.getId() == null) {
      throw new RuntimeException("单位ID为空");
    }
    Company coExample = new Company();
    coExample.setId(co.getId());
    coExample.setName(co.getName());
    Example<Company> example = Example.of(coExample);
    List<Company> list = companyDAO.findAll(example);
    for (Company company : list) {
      // if (company.getParent() == null){
      // break;
      // }
      List<Department> depts = departmentService.findAllByCompany(company.getId());
      if (!depts.isEmpty()) {
        throw new RuntimeException(String.format("单位:%s有下属部门不能删除！", company.getName()));
      }
    }
    companyDAO.deleteById(co.getId());
  }

  // 分页模糊查询
  public Page<Company> findAll(int page, int size, Company co) {
    ExampleMatcher matcher = ExampleMatcher.matching().withIgnoreNullValues().withMatcher("name",
        ExampleMatcher.GenericPropertyMatchers.contains());
    Example<Company> example = Example.of(co, matcher);
    Pageable pageable = PageRequest.of(page - 1, size, Sort.by(Sort.Direction.ASC, "parent", "orderindex"));
    return companyDAO.findAll(example, pageable);
  }

  // 查询全部
  public List<Company> findAll() {
    List<Company> list = companyDAO.findAll(Sort.by(Sort.Direction.ASC, "parent", "orderindex"));
    return list;
  }
}
